package Interface.Admin;

import Mapper.Admin.huiYuanMapper;
import Mapper.Admin.yuanGongMapper;
import Mapper.Admin.zhujiemian;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import pojo.huiyuan;
import pojo.yuangong;

import java.io.FileInputStream;

public class yuanGong extends Application {
    yuanGongMapper yuangongMapper;
    private static final int ITEMS_PER_PAGE = 15;
    private TableView<yuangong> tableView;
    private Pagination pagination;

    private TextField t_id;
    private TextField t_jieshao;
    private TextField t_shetuanming;
    @Override
    public void start(Stage stage) throws Exception {
        FileInputStream input=new FileInputStream("C:\\Users\\86178\\Videos\\Captures\\走路.gif");
        Image image = new Image(input);
        ImageView imageView =new ImageView(image);
        imageView.setFitWidth(488);
        imageView.setFitHeight(465);

        tableView = createTableView();
        pagination = new Pagination();

        Label id=new Label("id:");
        id.setFont(Font.font(12));
        t_id =new TextField();
        t_id.setPrefColumnCount(8);

        Label xingming=new Label("姓名:");
        xingming.setFont(Font.font(12));
        t_jieshao =new TextField();
        t_jieshao.setPrefColumnCount(8);

        Label huiyuanqingkuang=new Label("费用:");
        huiyuanqingkuang.setFont(Font.font(12));
        t_shetuanming =new TextField();
        t_shetuanming.setPrefColumnCount(8);

        Button gengxin =new Button("更新");
        gengxin.setOnAction(e -> gengxin());

        Button shanchu =new Button("删除");
        shanchu.setOnAction(e -> shanchu());

        Button tianjia =new Button("添加");
        tianjia.setOnAction(e -> tianjia());

        //实例化不然为null
        yuangongMapper =new yuanGongMapper();

        /**
         * 获取数据
         */
        pagination.setPageFactory(pageIndex -> {
            ObservableList<yuangong> data = yuangongMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });

        Pane pane = new Pane();
        //pane.getChildren().add(imageView);
        pane.getChildren().addAll(pagination,id,t_id,xingming,t_jieshao,huiyuanqingkuang,t_shetuanming,gengxin,tianjia,shanchu);

        id.setLayoutX(0);
        id.setLayoutY(20);
        t_id.setLayoutX(60);
        t_id.setLayoutY(18);

        huiyuanqingkuang.setLayoutX(0);
        huiyuanqingkuang.setLayoutY(50);
        t_shetuanming.setLayoutX(60);
        t_shetuanming.setLayoutY(48);

        xingming.setLayoutX(0);
        xingming.setLayoutY(80);
        t_jieshao.setLayoutX(60);
        t_jieshao.setLayoutY(78);

        gengxin.setLayoutX(0);
        gengxin.setLayoutY(150);

        tianjia.setLayoutX(50);
        tianjia.setLayoutY(150);

        shanchu.setLayoutX(100);
        shanchu.setLayoutY(150);

        pagination.setLayoutX(200);
        pagination.setLayoutY(16);

        Scene scene =new Scene(pane);
        stage.setScene(scene);
        stage.setTitle("员工管理");
        stage.setWidth(650);
        stage.setHeight(650);
        stage.setResizable(false);//窗口不可拉伸
        stage.show();
    }

    // 添加一个方法用于刷新数据
    private void refreshData() {
        int currentPageIndex = pagination.getCurrentPageIndex();
        pagination.setPageFactory(pageIndex -> {
            ObservableList<yuangong> data = yuangongMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });
        // 设置回之前的页数
        pagination.setCurrentPageIndex(currentPageIndex);
    }


    private void tianjia() {
        String xingming = t_jieshao.getText();
        String huiyuanqingkuang = t_shetuanming.getText();

        yuangongMapper.tianjia(xingming,huiyuanqingkuang);

        refreshData();
    }

    private void shanchu() {
        String id = t_id.getText();

        yuangongMapper.shanchu(id);
        refreshData();
    }

    private void gengxin() {
        String id = t_id.getText();
        String xingming = t_jieshao.getText();
        String huiyuanqingkuang = t_shetuanming.getText();

        yuangongMapper.gengxin(id,xingming,huiyuanqingkuang);
        refreshData();
    }

    private TableView<yuangong> createTableView() {
        TableView<yuangong> tableView = new TableView<>();

        TableColumn<yuangong, String> id = new TableColumn<>("id");
        id.setCellValueFactory(cellData -> cellData.getValue().idProperty());
        TableColumn<yuangong, String> name = new TableColumn<>("名字");
        name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        TableColumn<yuangong, String> createtime = new TableColumn<>("创建时间");
        createtime.setCellValueFactory(cellData -> cellData.getValue().createtimeProperty());
        TableColumn<yuangong, String> quanxianProperty = new TableColumn<>("权限");
        quanxianProperty.setCellValueFactory(cellData -> cellData.getValue().quanxianProperty());
        TableColumn<yuangong, String> zhiwuProperty = new TableColumn<>("职务");
        zhiwuProperty.setCellValueFactory(cellData -> cellData.getValue().zhiwuProperty());
        TableColumn<yuangong, String> feiyongProperty = new TableColumn<>("费用");
        feiyongProperty.setCellValueFactory(cellData -> cellData.getValue().feiyongProperty());


        tableView.getColumns().addAll(id, name, createtime, quanxianProperty,zhiwuProperty,feiyongProperty);

        return tableView;
    }
}